package com.party.mobile.biz.mall;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.mall.MallGoods;
import com.party.core.model.mall.MallShowGoods;
import com.party.core.model.mall.MallShowSubject;
import com.party.core.service.mall.IMallGoodsService;
import com.party.core.service.mall.IMallShowGoodsService;
import com.party.core.service.mall.IMallShowSubjectService;
import com.party.mobile.web.dto.mall.output.MallSiftGoodsOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.*;

/**
 * 展示商品业务处理
 * Created by yifeng
 *
 * @date 2018/5/28 0028
 * @time 上午 10:13
 */
@Service
public class MallShowGoodsBizService {
    @Autowired
    private IMallShowSubjectService mallShowSubjectService;
    @Autowired
    private IMallShowGoodsService mallShowGoodsService;
    @Autowired
    private IMallGoodsService mallGoodsService;

    /**
     * 获取分销商品列表
     *
     * @param memberId 用户id
     * @param page     分页参数
     * @return
     */
    public List<MallSiftGoodsOutput> getDistributorGoodsList(String memberId, Page page) {
        Set<String> goodsIds = new LinkedHashSet<String>();
        List<MallShowSubject> showSubjects = mallShowSubjectService.list(new MallShowSubject(memberId));
        if (showSubjects.size() == 0) {
            return new ArrayList<MallSiftGoodsOutput>();
        }
        List<String> subjectIds = LangUtils.transform(showSubjects, new Function<MallShowSubject, String>() {
            @Nullable
            @Override
            public String apply(@Nullable MallShowSubject input) {
                return input.getId();
            }
        });

        Map<String, Object> params = Maps.newHashMap();
        params.put("subjectIds", subjectIds);
        List<Map<String, Object>> showGoodList = mallShowGoodsService.getDistributorGoods(params);
        if (showGoodList.size() == 0) {
            return new ArrayList<MallSiftGoodsOutput>();
        }
        for (Map<String, Object> input : showGoodList) {
            Integer type = (Integer) input.get("type");
            String targetId = (String) input.get("targetId");
            if (type.equals(MallShowGoods.TYPE_CATEGORY)) { // 分类
                List<MallGoods> mallGoods = mallGoodsService.findByCategoryId(targetId);
                for (MallGoods mg : mallGoods) {
                    goodsIds.add(mg.getId());
                }
            } else if (type.equals(MallShowGoods.TYPE_GOODS)) { // 商品
                goodsIds.add(targetId);
            }
        }
        List<MallGoods> mallGoods = mallGoodsService.getGoodsList(goodsIds, page);
        List<MallSiftGoodsOutput> goodsOutputs = LangUtils.transform(mallGoods, new Function<MallGoods, MallSiftGoodsOutput>() {
            @Nullable
            @Override
            public MallSiftGoodsOutput apply(@Nullable MallGoods input) {
                MallSiftGoodsOutput goodsOutput = new MallSiftGoodsOutput();
                goodsOutput.setId(input.getId());
                goodsOutput.setName(input.getName());
                goodsOutput.setPic(input.getPic());
                goodsOutput.setStock(input.getRealStock());
                goodsOutput.setSalesNum(input.getSalesNum());
                goodsOutput.setRemarks(input.getRemarks());
                if (StringUtils.isNotEmpty(input.getPrice())) {
                    if (input.getPrice().indexOf("~") == -1) {
                        goodsOutput.setPrice(input.getPrice());
                    } else {
                        String[] priceArray = input.getPrice().split("~");
                        goodsOutput.setPrice(priceArray[0]);
                    }
                }
                if (StringUtils.isNotEmpty(input.getTradePrice())) {
                    if (input.getTradePrice().indexOf("~") == -1) {
                        goodsOutput.setTradePrice(input.getTradePrice());
                    } else {
                        String[] priceArray = input.getTradePrice().split("~");
                        goodsOutput.setTradePrice(priceArray[0]);
                    }
                }
                return goodsOutput;
            }
        });
        return goodsOutputs;
    }
}
